package com.ruoyi.lxgl.kylx.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lxgl.kylx.domain.*;
import com.ruoyi.lxgl.kylx.mapper.LlglXmsbMapper;
import com.ruoyi.lxgl.kylx.service.ILlglXmsbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目申报Service业务层处理
 *
 * @author huacai
 * @date 2024-03-14
 */
@Service
public class LlglXmsbServiceImpl implements ILlglXmsbService {
    @Resource
    private LlglXmsbMapper llglXmsbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询项目申报
     *
     * @param xmsbId 项目申报主键
     * @return 项目申报
     */
    @Override
    public LlglXmsb selectLlglXmsbByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbByXmsbId(xmsbId);
    }

    /**
     * 获取项目申报与项目计划进度和目标详细信息
     * @param xmsbId 项目申报主键
     * @return 项目申报
     */
    @Override
    public LlglXmsb selectLlglXmsbByjhjdByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbByjhjdByXmsbId(xmsbId);
    }

    /**
     * 获取项目申报与研发费用预算上信息信息
     * @param xmsbId 项目申报主键
     * @return 项目申报
     */
    @Override
    public LlglXmsb selectLlglXmsbByyffyByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbByyffyByXmsbId(xmsbId);
    }

    /**
     * 查询项目申报列表
     *
     * @param llglXmsb 项目申报
     * @return 项目申报
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<LlglXmsb> selectLlglXmsbList(LlglXmsb llglXmsb) {
        return llglXmsbMapper.selectLlglXmsbList(llglXmsb);
    }

    /**
     * 查询项目申报列表 (项目申报页面使用)
     * @param llglXmsb
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<LlglXmsb> getXmsbList(LlglXmsb llglXmsb) {
        return llglXmsbMapper.getXmsbList(llglXmsb);
    }

    /**
     * 新增项目申报
     *
     * @param llglXmsb 项目申报
     * @return 结果
     */
    @Transactional
    public int insertLlglXmsb(LlglXmsb llglXmsb) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 插入部门ID
        llglXmsb.setDeptId(Math.toIntExact(loginUser.getUser().getDeptId()));
        // 创建人
        llglXmsb.setCreateBy(loginUser.getUsername());
        int rows = llglXmsbMapper.insertLlglXmsb(llglXmsb);
        insertLxglXmsbFj(llglXmsb);
//        insertLxglXmsbJhjd(llglXmsb);
        insertLxglXmsbYffy(llglXmsb);
        insertLxglXmsbYffyys(llglXmsb);
        insertLxglXmsbYfcl(llglXmsb);
        insertLxglXmsbYfsb(llglXmsb);
        insertLxglXmsbZjsyf(llglXmsb);
        insertLxglXmsbZcry(llglXmsb);
        return rows;
    }

    /**
     * 批量新增项目申报
     *
     * @param llglXmsbs 项目申报List
     * @return 结果
     */
    @Override
    public int batchInsertLlglXmsb(List<LlglXmsb> llglXmsbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(llglXmsbs)) {
            try {
                for (int i = 0; i < llglXmsbs.size(); i++) {
                    int row = llglXmsbMapper.insertLlglXmsb(llglXmsbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == llglXmsbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改项目申报
     *
     * @param llglXmsb 项目申报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLlglXmsb(LlglXmsb llglXmsb) {
        llglXmsb.setUpdateTime(DateUtils.getNowDate());
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 修改人
        llglXmsb.setUpdateBy(loginUser.getUsername());
        llglXmsbMapper.deleteLxglXmsbFjByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbJhjdByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbYffyByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbYffyysByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbYfclByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbYfsbByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbZjsyfByXmsbId(llglXmsb.getXmsbId());
        llglXmsbMapper.deleteLxglXmsbZcryByXmsbId(llglXmsb.getXmsbId());  //删除研究课题组成人员情况信息
        insertLxglXmsbFj(llglXmsb);
        insertLxglXmsbJhjd(llglXmsb);
        insertLxglXmsbYffy(llglXmsb);
        insertLxglXmsbYffyys(llglXmsb);
        insertLxglXmsbYfcl(llglXmsb);
        insertLxglXmsbYfsb(llglXmsb);
        insertLxglXmsbZjsyf(llglXmsb);
        insertLxglXmsbZcry(llglXmsb); //新增研究课题组成人员情况信息
        return llglXmsbMapper.updateLlglXmsb(llglXmsb);
    }

    /**
     * 批量删除项目申报
     *
     * @param xmsbIds 需要删除的项目申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLlglXmsbByXmsbIds(Integer[] xmsbIds) {
        llglXmsbMapper.deleteLxglXmsbFjByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbJhjdByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbYffyByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbYffyysByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbYfclByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbYfsbByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbZjsyfByXmsbIds(xmsbIds);
        llglXmsbMapper.deleteLxglXmsbZcryByXmsbIds(xmsbIds);
        return llglXmsbMapper.deleteLlglXmsbByXmsbIds(xmsbIds);
    }

    /**
     * 删除项目申报信息
     *
     * @param xmsbId 项目申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLlglXmsbByXmsbId(Integer xmsbId) {
        llglXmsbMapper.deleteLxglXmsbFjByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbJhjdByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbYffyByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbYffyysByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbYfclByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbYfsbByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbZjsyfByXmsbId(xmsbId);
        llglXmsbMapper.deleteLxglXmsbZcryByXmsbId(xmsbId);
        return llglXmsbMapper.deleteLlglXmsbByXmsbId(xmsbId);
    }

    /**
     * 新增附件
     * @param lxglXmsbFj 附件
     * @return 结果
     */
    @Override
    public int insertLxglXmsbFjByxmsbId(LxglXmsbFj lxglXmsbFj) {
        return llglXmsbMapper.insertLxglXmsbFjByxmsbId(lxglXmsbFj);
    }

    /**
     * 根据主表ID查询附件列表
     * @param lxglXmsbFj 附件
     * @return 结果
     */
    @Override
    public List<LxglXmsbFj> selectLxglXmsbFjList(LxglXmsbFj lxglXmsbFj) {
        return llglXmsbMapper.selectLxglXmsbFjList(lxglXmsbFj);
    }

    /**
     * 根据id删除附件
     * @param fjId 附件主键
     * @return
     */
    @Override
    public int deleteLxglXmsbFjByFjId(Integer fjId) {
        return llglXmsbMapper.deleteLxglXmsbFjByFjId(fjId);
    }

    /**
     * 查询当前最新的主表主键ID
     *
     * @return
     */
    @Override
    public int selectMAXId() {
        return llglXmsbMapper.selectMAXId();
    }

    @Override
    public Map<String, Integer> getJhjdMaxAndMinNum(Integer xmsbId) {
        return llglXmsbMapper.getJhjdMaxAndMinNum(xmsbId);
    }

    @Override
    public List<LxglXmsbJhjd> getJhjdList(Integer xmsbId) {
        return llglXmsbMapper.getJhjdList(xmsbId);
    }

    @Override
    public int updateLxglXmsbJhjd(LxglXmsbJhjd lxglXmsbJhjd) {
        return llglXmsbMapper.updateLxglXmsbJhjd(lxglXmsbJhjd);
    }

    /**
     * 获取项目申报与研发费用预算下信息信息
     * @param xmsbId
     * @return
     */
    @Override
    public LlglXmsb selectLlglXmsbByyffysByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbByyffysByXmsbId(xmsbId);
    }

    /**
     * selectLlglXmsbByYfclByXmsbId
     * @param xmsbId
     * @return
     */
    @Override
    public LlglXmsb selectLlglXmsbByYfclByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbByYfclByXmsbId(xmsbId);
    }

    /**
     * 获取项目申报与研发费用研发材料
     * @param xmsbId
     * @return
     */
    @Override
    public LlglXmsb selectLlglXmsbYfsbByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbYfsbByXmsbId(xmsbId);
    }

    /**
     * 获取项目申报与研发费用中间试验费
     * @param xmsbId
     * @return
     */
    @Override
    public LlglXmsb selectLlglXmsbZjsyfByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbZjsyfByXmsbId(xmsbId);
    }

    /**
     * 获取项目申报与课题组成人员
     * @param xmsbId
     * @return
     */
    @Override
    public LlglXmsb selectLlglXmsbZcryByXmsbId(Integer xmsbId) {
        return llglXmsbMapper.selectLlglXmsbZcryByXmsbId(xmsbId);
    }

    /**
     * 课题归属转换
     * @param llglXmsb
     * @return
     */
    @Override
    public int updateKtgs(LlglXmsb llglXmsb) {
        return llglXmsbMapper.updateKtgs(llglXmsb);
    }

    /**
     * 根据项目所属的部门ID查询它的顶级ID (非100)(分局ID)
     * @param xmDeptId
     * @return
     */
    @Override
    public Integer selectDjIdByXmDeptId(Integer xmDeptId) {
        return llglXmsbMapper.selectDjIdByXmDeptId(xmDeptId);
    }

    /**
     * 新增附件信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbFj(LlglXmsb llglXmsb) {
        List<LxglXmsbFj> lxglXmsbFjList = llglXmsb.getLxglXmsbFjList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbFjList)) {
            List<LxglXmsbFj> list = new ArrayList<LxglXmsbFj>();
            for (LxglXmsbFj lxglXmsbFj : lxglXmsbFjList) {
                lxglXmsbFj.setXmsbId(xmsbId);
                list.add(lxglXmsbFj);
            }
            if (list.size() > 0) {
                llglXmsbMapper.batchLxglXmsbFj(list);
            }
        }
    }

    /**
     * 新增项目计划进度和目标信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbJhjd(LlglXmsb llglXmsb) {
        List<LxglXmsbJhjd> lxglXmsbJhjdList = llglXmsb.getLxglXmsbJhjdList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbJhjdList)) {
            List<LxglXmsbJhjd> list = new ArrayList<LxglXmsbJhjd>();
            for (LxglXmsbJhjd lxglXmsbJhjd : lxglXmsbJhjdList) {
                lxglXmsbJhjd.setXmsbId(xmsbId);
                lxglXmsbJhjd.setJhjdId(null);
                list.add(lxglXmsbJhjd);
            }
            if (list.size() > 0) {
                llglXmsbMapper.batchLxglXmsbJhjd(list);
            }
        }
    }

    /**
     * 新增研发费用预算上信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbYffy(LlglXmsb llglXmsb) {
        List<LxglXmsbYffy> lxglXmsbYffyList = llglXmsb.getLxglXmsbYffyList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbYffyList)) {
            List<LxglXmsbYffy> list = new ArrayList<LxglXmsbYffy>();
            for (LxglXmsbYffy lxglXmsbYffy : lxglXmsbYffyList) {
                lxglXmsbYffy.setXmsbId(xmsbId);
                list.add(lxglXmsbYffy);
            }
            if (list.size() > 0) {
                llglXmsbMapper.batchLxglXmsbYffy(list);
            }
        }
    }

    /**
     * 新增研发费用预算下信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbYffyys(LlglXmsb llglXmsb)
    {
        List<LxglXmsbYffyys> lxglXmsbYffyysList = llglXmsb.getLxglXmsbYffyysList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbYffyysList))
        {
            List<LxglXmsbYffyys> list = new ArrayList<LxglXmsbYffyys>();
            for (LxglXmsbYffyys lxglXmsbYffyys : lxglXmsbYffyysList)
            {
                lxglXmsbYffyys.setXmsbId(xmsbId);
                list.add(lxglXmsbYffyys);
            }
            if (list.size() > 0)
            {
                llglXmsbMapper.batchLxglXmsbYffyys(list);
            }
        }
    }

    /**
     * 新增直接用于研发的主要材料信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbYfcl(LlglXmsb llglXmsb)
    {
        List<LxglXmsbYfcl> lxglXmsbYfclList = llglXmsb.getLxglXmsbYfclList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbYfclList))
        {
            List<LxglXmsbYfcl> list = new ArrayList<LxglXmsbYfcl>();
            for (LxglXmsbYfcl lxglXmsbYfcl : lxglXmsbYfclList)
            {
                lxglXmsbYfcl.setXmsbId(xmsbId);
                list.add(lxglXmsbYfcl);
            }
            if (list.size() > 0)
            {
                llglXmsbMapper.batchLxglXmsbYfcl(list);
            }
        }
    }

    /**
     * 新增设备（专门用于研发主要仪器、设备清单）信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbYfsb(LlglXmsb llglXmsb)
    {
        List<LxglXmsbYfsb> lxglXmsbYfsbList = llglXmsb.getLxglXmsbYfsbList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbYfsbList))
        {
            List<LxglXmsbYfsb> list = new ArrayList<LxglXmsbYfsb>();
            for (LxglXmsbYfsb lxglXmsbYfsb : lxglXmsbYfsbList)
            {
                lxglXmsbYfsb.setXmsbId(xmsbId);
                list.add(lxglXmsbYfsb);
            }
            if (list.size() > 0)
            {
                llglXmsbMapper.batchLxglXmsbYfsb(list);
            }
        }
    }

    /**
     * 新增中间试验费信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbZjsyf(LlglXmsb llglXmsb)
    {
        List<LxglXmsbZjsyf> lxglXmsbZjsyfList = llglXmsb.getLxglXmsbZjsyfList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbZjsyfList))
        {
            List<LxglXmsbZjsyf> list = new ArrayList<LxglXmsbZjsyf>();
            for (LxglXmsbZjsyf lxglXmsbZjsyf : lxglXmsbZjsyfList)
            {
                lxglXmsbZjsyf.setXmsbId(xmsbId);
                list.add(lxglXmsbZjsyf);
            }
            if (list.size() > 0)
            {
                llglXmsbMapper.batchLxglXmsbZjsyf(list);
            }
        }
    }

    /**
     * 新增研究课题组成人员情况信息
     *
     * @param llglXmsb 项目申报对象
     */
    public void insertLxglXmsbZcry(LlglXmsb llglXmsb) {
        List<LxglXmsbZcry> lxglXmsbZcryList = llglXmsb.getLxglXmsbZcryList();
        Integer xmsbId = llglXmsb.getXmsbId();
        if (StringUtils.isNotNull(lxglXmsbZcryList)) {
            List<LxglXmsbZcry> list = new ArrayList<LxglXmsbZcry>();
            for (LxglXmsbZcry lxglXmsbZcry : lxglXmsbZcryList) {
                lxglXmsbZcry.setXmsbId(xmsbId);
                lxglXmsbZcry.setZcryId(null); // 确保使用新的自增主键
                list.add(lxglXmsbZcry);
            }
            if (list.size() > 0) {
                llglXmsbMapper.batchLxglXmsbZcry(list);
            }
        }
    }

}
